package com.chen.getaway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class GulimallCorsConfiguration {
    //配置跨域
    //js默认出于保护机制则不会跨域访问，需要自己设置响应头去允许浏览器访问
    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration configuration = new CorsConfiguration();
        //允许所有请求头的跨域访问
        configuration.addAllowedHeader("*");
        //允许所有请求方式的跨域访问
        configuration.addAllowedMethod("*");
        //允许所有来源的访问
        configuration.addAllowedOrigin("*");
        //设置允许返回时携带cooking
        configuration.setAllowCredentials(true);
        //调用注册跨域配置方法
        source.registerCorsConfiguration("/**",configuration);
        return new CorsWebFilter(source);
    }
}
